/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sv.com.intesal.compra.controlador;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import sv.com.intesal.clases.abstracts.ControladorAbstract;
import sv.com.intesal.compra.bean.Linea;
import sv.com.intesal.compra.bean.SubLinea;
import sv.com.intesal.compra.sql.SQLSubLinea;

/**
 *
 * @author ejerez
 */
public class SubLineaControlador extends ControladorAbstract {

    public void insert(SubLinea sl) throws Exception {
        try {
            super.setPreparedStatement(SQLSubLinea.insert);
            super.ps.setInt(1, sl.getCodigo_l());
            super.ps.setString(2, sl.getNombre_sl());
            super.ps.setString(3, super.v.getEstado(sl.getEstado_filtro_sl()));
            super.ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public void update(SubLinea sl) throws Exception {
        try {
            super.setPreparedStatement(SQLSubLinea.update);
            super.ps.setInt(1, sl.getCodigo_l());
            super.ps.setString(2, sl.getNombre_sl());
            super.ps.setString(3, super.v.getEstado(sl.getEstado_filtro_sl()));
            super.ps.setInt(4, sl.getCodigo_sl());
            super.ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public List<SubLinea> getSubLineas() throws Exception {
        List<SubLinea> lista = new ArrayList<SubLinea>();
        try {
            this.getExecuteQuery(SQLSubLinea.getSubLineas);
            while (super.rs.next()) {
                lista.add(this.getSubLinea(super.rs));
            }
            return lista;
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public List<SubLinea> getSubLineaActivos() throws Exception {
        List<SubLinea> lista = new ArrayList<SubLinea>();
        try {
            this.getExecuteQuery(SQLSubLinea.getSubLineaActivas);
            while (super.rs.next()) {
                lista.add(this.getSubLinea(super.rs));
            }
            return lista;
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public List<SubLinea> getSubLineaActivos_x_Linea(Integer codigo_l) throws Exception {
        List<SubLinea> lista = new ArrayList<SubLinea>();
        try {
            super.setPreparedStatement(SQLSubLinea.getSubLineaActivas_x_Linea);
            super.ps.setInt(1, codigo_l);
            super.rs = super.ps.executeQuery();
            while (super.rs.next()) {
                lista.add(this.getSubLinea(super.rs));
            }
            return lista;
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    private SubLinea getSubLinea(ResultSet rs) throws Exception {
        try {
            SubLinea sl = new SubLinea();
            sl.setCodigo_sl(rs.getInt("codigo_sl"));
            sl.setNombre_sl(rs.getString("nombre_sl"));
            sl.setEstado_sl(rs.getString("estado_sl"));
            sl.setEstado_filtro_sl(v.getEstado(rs.getString("estado_sl")));

            sl.setNombre_l(rs.getString("nombre_l"));
            sl.setCodigo_l(rs.getInt("codigo_l"));
            return sl;
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    public List<Linea> getLineasActivos() throws Exception {
        try {
            LineaControlador lc = new LineaControlador();
            return lc.getLineasActivos();
        } catch (Exception e) {
            throw new Exception(e);
        }
    }
}
